package repository

import (
	"context"

	"server/internal/app/model"
	"server/internal/core/store"

	"gorm.io/gorm"
)

type SysPermissionRepository struct {
	db *gorm.DB
}

func NewSysPermissionRepository() *SysPermissionRepository {
	return &SysPermissionRepository{db: store.DB}
}

func (r *SysPermissionRepository) All(ctx context.Context) ([]model.SysPermission, error) {
	var permissions []model.SysPermission
	err := r.db.WithContext(ctx).Find(&permissions).Error
	return permissions, err
}

func (r *SysPermissionRepository) Create(ctx context.Context, permission *model.SysPermission) error {
	return r.db.WithContext(ctx).Create(permission).Error
}

func (r *SysPermissionRepository) Update(ctx context.Context, id uint, data map[string]any) error {
	return r.db.WithContext(ctx).Model(&model.SysPermission{}).Where("id = ?", id).Updates(data).Error
}

func (r *SysPermissionRepository) Delete(ctx context.Context, id uint) error {
	return r.db.WithContext(ctx).Delete(&model.SysPermission{}, id).Error
}
